package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarBrakeHand;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

/**
 * 普通手刹和怠速手刹算法<br/>
 * 依赖{@link Mileage}里程油耗算法<br/>
 * 依赖{@link IdleParking}的怠速状态<br/>
 * 输出：统计<br/>
 */
public class BrakeHand extends AbstractArithmetic<VarBrakeHand> {

    @Override
    public void calculate() {
        logger.debug("进入手刹算法[terminalId:{}]", terminalId);
        VarBrakeHand var = getObj();
        CommonData commonData = getCommonData();

        // 当前点是否打开手刹
        boolean curStatus = commonData.isHandBrake();

        //判断是否为怠速状态下
        if (commonData.isParkingIdle()) {
            logger.debug("手刹算法，怠速状态。[terminalId:{}]", terminalId);
            boolean preState = var.isPreParkingIdleHandbrake();
            var.setPreDrivingHandbrake(false);
            var.setPreParkingIdleHandbrake(curStatus);

            //上一次和本次都未手刹，跳出算法
            if (!preState && !curStatus) {
                logger.debug("手刹算法，手刹状态：上一次和本次都未手刹，跳出算法.[terminalId:{}]", terminalId);
                return;
            }

            //算法计算
            switch (super.getEventStatus(preState, curStatus)) {
                case START:
                    logger.debug("手刹算法（怠速状态）事件开始.[terminalId:{}]", terminalId);
                    var.setParkingIdleFrequency(var.getParkingIdleFrequency() + 1);
                    break;
                case IN:
                case END:
                    logger.debug("手刹算法（怠速状态）算法，事件中或结束.[terminalId:{}]", terminalId);
                    var.setParkingIdleTimeMs(var.getParkingIdleTimeMs() + commonData.getDffTime());
                    break;
                default:
            }
        }
        //非怠速状态
        else {
            logger.debug("手刹算法，行驶状态。[terminalId:{}]", terminalId);
            boolean preState = var.isPreDrivingHandbrake();
            var.setPreDrivingHandbrake(curStatus);
            var.setPreParkingIdleHandbrake(false);

            //上一次和本次都未手刹，跳出算法
            if (!preState && !curStatus) {
                logger.debug("手刹算法，手刹状态：上一次和本次都未手刹，跳出算法.[terminalId:{}]", terminalId);
                return;
            }

            //算法计算
            switch (super.getEventStatus(preState, curStatus)) {
                case START:
                    logger.debug("手刹算法（行驶中状态）事件开始.[terminalId:{}]", terminalId);
                    //手刹算法次数+1
                    var.setDrivingFrequency(var.getDrivingFrequency() + 1);
                    break;
                case IN:
                case END:
                    logger.debug("手刹算法（行驶中状态）算法，事件中或结束.[terminalId:{}]", terminalId);
                    var.setDrivingTimeMs(var.getDrivingTimeMs() + commonData.getDffTime());
                    var.setDrivingMileage(var.getDrivingMileage() + commonData.getDffMileage());
                    break;
                default:
                    break;
            }
        }

        logger.debug("离开手刹算法[terminalId:{}]", terminalId);
    }


    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarBrakeHand var = getObj();
        statisticDataOut.setDrivingHandbrakeNumber(var.getDrivingFrequency());
        statisticDataOut.setDrivingHandbrakeMileage(ArithmeticUtils.mileageMToM(var.getDrivingMileage()));
        statisticDataOut.setDrivingHandbrakeDuration(ArithmeticUtils.timeMsToSecond(var.getDrivingTimeMs()));

        statisticDataOut.setIdleHandbrakeNumber(var.getParkingIdleFrequency());
        statisticDataOut.setIdleHandbrakeDuration(ArithmeticUtils.timeMsToSecond(var.getParkingIdleTimeMs()));
    }
}
